//
//  NSAddressController.m
//  The Night Shift
//
//  Created by 张城 on 16/7/5.
//  Copyright © 2016年 张城. All rights reserved.
//

#import "NSAddressController.h"
#import "NSProvincesModel.h"
#import "NSCityController.h"


@interface NSAddressController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray * dataArray;

@end

@implementation NSAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"当前省份位置";
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"%ld",self.dataArray.count);
    return self.dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ss = @"123456";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ss];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ss];
    }
    
    NSProvincesModel * model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSCityController * cc = [[NSCityController alloc]init];

    NSProvincesModel * model = self.dataArray[indexPath.row];
    
    cc.cityArray = model.cities;

    [self jumpToOtherViewController:cc];
}


#pragma mark - 省份的懒加载
-(NSArray*)dataArray{
    if (nil == _dataArray) {
        NSString * path = [[NSBundle mainBundle]pathForResource:@"province.plist" ofType:nil];
        NSArray * temp = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray * muss = [NSMutableArray array];
        for (NSDictionary * dic in temp) {
            NSProvincesModel * model = [NSProvincesModel provinceModelWithDic:dic];
            
            [muss addObject:model];
        }
        _dataArray = muss;
    }
    return _dataArray;
}

#pragma mark - tableView的懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)dealloc {

    NSLog(@"没了");
}

@end
